package com.red.wine.webservice;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.MalformedURLException;

import android.util.Log;

import com.google.json.Gson;
import com.red.wine.http.HttpHelper;
import com.red.wine.sql.UserLog;
import com.red.wine.util.StringUtil;

public abstract class BaseService {

	private static final String TAG = "BaseService";
	protected String postOnService(String urlString, String objectString)
			throws MalformedURLException, IOException {
		Log.e("send", "url: = " + urlString + " , content: = " + objectString);
		Log.e("TAG", "bodyString: = " + objectString);
		HttpHelper http = new HttpHelper(objectString.length());
		http.createMode().url(urlString) 
				.header("Connection", "Keep-Alive")
				.header("Content-type", "application/x-www-form-urlencoded");
		
		http.body(new ByteArrayInputStream(objectString.getBytes())).send();

		String bodyString = "";
		if (http.isSuccessful()) {
			bodyString = http.getBodyAsString();
//			Log.e("返回字符串", "返回字符串: = " + bodyString);
			Log.e("返回字符串", "bodyString: = " + StringUtil.unicodeToUtf8(bodyString));
			UserLog.getInstance().write(StringUtil.unicodeToUtf8(bodyString));
			if (bodyString != null) {
				return bodyString;
			} else {
				return "";
			}
		} else {
			final int responseCode = http.getResponseCode();
			Log.e("TAG", "连接不成功 返回码: = " + responseCode);
		}
		return null;

	}
}
